package org.hackystat.devcathlon.ui.page.matches.manage.matchpanel;

import java.util.Iterator;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.Model;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.matches.manage.matchconfigurationpanel
.MatchConfigurationPage;
import org.hackystat.devcathlon.ui.page.matches.manage.matchdeletepanel.MatchDeletePage;
import org.hackystat.devcathlon.ui.page.teams.profile.TeamsProfile;
import org.hackystat.utilities.tstamp.Tstamp;

/**
 * The panel of matches for the match manage page.
 * 
 * @author robin
 * 
 */
public class MatchPanel extends Panel {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  private String noMatches;

  /**
   * Constructs the match panel for match manager page.
   * 
   * @param id Component id.
   * @param listItem The list of matches.
   */
  public MatchPanel(String id, List<String> listItem) {
    super(id);

    // Check if there are any matches, if no set the label.
    if (listItem.isEmpty()) {
      noMatches = "There are no matches";
    }

    DataView dataview = new DataView("ItemList", new ListDataProvider(listItem)) {
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(Item item) {
        final String matchName = (String) item.getModelObject();
        item.add(new Label("MatchLabel", matchName));
        item.add(new Link("MatchLink") {
          private static final long serialVersionUID = 1L;

          /** Upon clicking this link, go to Match page */
          @Override
          public void onClick() {
            try {
              setResponsePage(new MatchConfigurationPage(MatchManager.getInstance().getMatch(
                  matchName)));
            }
            catch (Exception e) {
              e.printStackTrace();
            }
          }
        });

        // Make start time
        XMLGregorianCalendar now = Tstamp.makeTimestamp();
        String startStringTime = " ";
        String endStringTime = "";
        String status = "";
        try {
       
          startStringTime = MatchManager.getInstance().getMatch(matchName).printStartDateTime();
          endStringTime = MatchManager.getInstance().getMatch(matchName).printEndDateTime();
          
          if ((Tstamp.lessThan(now, MatchManager.getInstance().getMatch(matchName)
              .getStartTime())) && (Tstamp.lessThan(now, MatchManager.getInstance().
                  getMatch(matchName).getEndTime()))) {
            status = "Pending";
          }
          else if ((Tstamp.lessThan(now, MatchManager.getInstance().getMatch(matchName)
              .getEndTime())) && (Tstamp.lessThan(MatchManager.getInstance().getMatch(matchName)
                  .getStartTime(), now))) {
            status = "On Going";
          }
          else {
            status = "Completed";
          }
          
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        item.add(new Label("StatusLabel", status));
        item.add(new Label("StartTimeLabel", startStringTime));
        item.add(new Label("EndTimeLabel", endStringTime));
        item.add(new Link("deleteMatchLink") {
          private static final long serialVersionUID = 1L;

          @Override
          public void onClick() {
            // DevcathlonSession.get().getMatchesSession().
            DevcathlonSession.get().getMatchesSession().setSelectedMatch(
                MatchManager.getInstance().getMatch(matchName).getName());
            setResponsePage(new MatchDeletePage(MatchManager.getInstance().getMatch(matchName)));
          }
        });

        WebMarkupContainer row1 = new WebMarkupContainer("Team1Row");
        WebMarkupContainer row2 = new WebMarkupContainer("Team2Row");
        if (status.compareTo("Completed") == 0) {
          if (1 > 0) {
            row1.add(new AttributeModifier("bgcolor", new Model("#6B90DB")));
          }
          else {
            row2.add(new AttributeModifier("bgcolor", new Model("#6B90DB")));
          }
        }

        // later check to see how much teams there can be for each match
        // Iterator<Team> it = mItem.getTeams().iterator();
        Iterator<Team> iTeam = MatchManager.getInstance().getMatch(matchName).getTeams().iterator();
        final Team team1 = iTeam.next();
        row1.add(new Link("Team1Link") {
          private static final long serialVersionUID = 1L;

          /** Upon clicking this link, go to Team page. */
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team1.getName());
            setResponsePage(new TeamsProfile());
          }
        }
        .add(new Label("Team1Label", team1.getName())));
        Integer team1Points = MatchManager.getInstance().getMatch(matchName).getTeamPoints(team1);
        row1.add(new Label("Team1PointsLabel", team1Points.toString()));
        // row1.add(new Label("Team1PointsLabel", ""));
        final Team team2 = iTeam.next();
        row2.add(new Link("Team2Link") {
          private static final long serialVersionUID = 1L;

          /** Upon clicking this link, go to Team page. */
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team2.getName());
            setResponsePage(new TeamsProfile());
          }
        }
        .add(new Label("Team2Label", team2.getName())));
        Integer team2Points = MatchManager.getInstance().getMatch(matchName).getTeamPoints(team2);
        row2.add(new Label("Team2PointsLabel", team2Points.toString()));
        // row2.add(new Label("Team2PointsLabel", ""));

        item.add(row1);
        item.add(row2);
      }
    };
    add(new Label("noMatches", noMatches));
    dataview.setItemsPerPage(5);
    add(dataview);
    add(new PagingNavigator("matchTopNavigator", dataview));
    add(new PagingNavigator("matchBottomNavigator", dataview));
  }
}
